//
//  LifeViewController.m
//  一点互助
//
//  Created by mac on 16/9/2.
//  Copyright © 2016年 zhangboqiu. All rights reserved.
//

#import "LifeViewController.h"
#import "FTHomeTableViewCell.h"
#import "ResponseManager.h"
#import "BiggerTableViewCell.h"
#import "DetailViewController.h"
#import "UIViewController+NavagitionBar.h"
@interface LifeViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataSource;
@property (nonatomic,strong) FTHomeTableViewCell *cell;

@end

@implementation LifeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupBackItem];
    [self setupTabelView];
    [self.view addSubview:self.tableView];
    UINib *nib = [UINib nibWithNibName:@"BiggerTabelViewCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"BiggerTabelViewCell"];
    [self getWishNetwork];
    
    
}

-(void)setupTabelView {
    
}

-(void)getWishNetwork{
    NSDictionary *dic = @{@"wish_cat":@(1)};
    __weak typeof(self) myself = self;
    [ResponseManager startGetRequestWithURLString:kGetWishListURL parameters:dic completionHandler:^(FBResponse *response) {
        //        stop1 = true;
        if (response.success) {
            PLLOG(@"%@",response.resultValue);
            //            [myself checkRequestFinished];
            NSArray *arr = [WishesModel modelArrayWithDictionaryArr:response.resultValue];
            myself.dataSource = [arr mutableCopy];
            //            [myself.tableView reloadData];
            [myself.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
        }else{
            [self addAlertControllerWithTitle:@"提示" message:@"请求失败" actionNum:2 style:UIAlertControllerStyleAlert handler:^(UIAlertAction *action) {
                [myself getWishNetwork];
            }];
        }
    }];
}

#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120 * kScreenHeightScale;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BiggerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BiggerTabelViewCell" forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
//    cell.avatarImage.frame = CGRectMake(0, 0, 90, 90);
//    cell.avatarImage.layer.cornerRadius = cell.avatarImage.bounds.size.height/2;
//    self.cell = cell;
    return cell;
}
-(void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    self.cell.avatarImage.frame = CGRectMake(0, 0, 120, 120);
    self.cell.avatarImage.layer.cornerRadius = self.cell.avatarImage.bounds.size.height/2;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailViewController *vc = [[DetailViewController alloc]init];
    vc.model = self.dataSource[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor whiteColor];
    }
    return _tableView;
}

-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

@end
